using System;
using System.Web;
using System.Web.Configuration;
using System.Drawing;
using System.Configuration;
using System.Web.UI.DataVisualization.Charting;

namespace ODM2KMZ
{
    public partial class DataStreamers_WaterOneFlowChartStreamer : System.Web.UI.Page
    {
        //Setup and initialise variables
        static string currentURL = HttpContext.Current.Request.QueryString["URL"] == null ? ConfigurationManager.AppSettings["defaultWofService"] : HttpContext.Current.Request.QueryString["URL"];
        string endDate = HttpContext.Current.Request.QueryString["EndDate"] == null ? string.Empty : HttpContext.Current.Request.QueryString["EndDate"];
        string begindate = HttpContext.Current.Request.QueryString["BeginDate"] == null ? string.Empty : HttpContext.Current.Request.QueryString["BeginDate"];
        string siteCode = HttpContext.Current.Request.QueryString["SiteCode"] == null ? string.Empty : HttpContext.Current.Request.QueryString["SiteCode"];
        string variableCode = HttpContext.Current.Request.QueryString["VariableCode"] == null ? string.Empty : HttpContext.Current.Request.QueryString["VariableCode"];

        public string CurrentURL
        {
            get
            {

                //return Request.QueryString["URL"] == null ? ConfigurationManager.AppSettings["defaultWofService"] : Request.QueryString["URL"];
                return currentURL;
            }
            set
            {
                currentURL = value;
            }
        }
        public string Mode
        {
            get
            {
                return Request.QueryString["Mode"] == null ? "0" : Request.QueryString["Mode"];
            }
        }
        public string SiteCode
        {
            get
            {
                return siteCode;
            }
            set
            {
                siteCode = value;
            }

        }
        public string strPath
        {
            get
            {
                string URL = Request.Url.AbsoluteUri.Contains("?") ? Request.Url.AbsoluteUri.Remove(Request.Url.AbsoluteUri.IndexOf("?")) : Request.Url.AbsoluteUri;
                URL = URL.Replace(Request.Url.AbsolutePath, "") + Request.ApplicationPath;

                return URL;
            }
        }

        public string VariableCode
        {
            get
            {
                return variableCode;
            }
            set
            {
                variableCode = value;
            }

        }
        public string BeginDate
        {
            get
            {
                return begindate;
            }
            set
            {
                begindate = value;
            }

        }
        public string EndDate
        {
            get
            {
                return endDate;
            }
            set
            {
                endDate = value;
            }

        }

        protected void Page_Load(object sender, EventArgs e)
        {

            //Load template for chart
            chtOdmData.LoadTemplate(Properties.Settings.Default.GraphTemplate);


        }
        protected void Page_PreRender(object sender, EventArgs e)
        {

            // Get reference to service
            WaterOneFlow CurrentService = new WaterOneFlow();
            CurrentService.Url = CurrentURL;
            
            //Enable COmpression
            CurrentService.EnableDecompression = true;
           
            TimeSeriesResponseType Data = CurrentService.GetValuesObject(SiteCode, VariableCode, BeginDate, EndDate, string.Empty);
            chtOdmData.Series["ODMData"].Points.Clear();

            //Setup Chart
            foreach (ValueSingleVariable CurrentValue in Data.timeSeries.values.value)
            {
                chtOdmData.Series["ODMData"].Points.AddXY(CurrentValue.dateTime, CurrentValue.Value);
            }
            chtOdmData.Titles["TitleSiteName"].Text = ((SiteInfoType)Data.timeSeries.sourceInfo).siteName;
            chtOdmData.Titles["TitleSiteCode"].Text = Data.queryInfo.criteria.locationParam;
            chtOdmData.Titles["TitleParameter"].Text = Data.timeSeries.variable.variableName;
            chtOdmData.ChartAreas["Default"].AxisY.Title = Data.timeSeries.variable.units.Value;
        }
        protected void Page_Unload(object sender, EventArgs e)
        {
        }
    }
}